package testLayer;

import static org.junit.Assert.*;

import java.sql.SQLException;

import modelLayer.Block;
import modelLayer.Schedule;
import modelLayer.Teacher;
import modelLayer.TeachingBlock;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import dbLayer.DBBlock;
import dbLayer.DBClass;
import dbLayer.DBSchedule;
import dbLayer.DBTeacher;
import dbLayer.DBTeachingBlock;

public class BlockDatabaseTest {
	
	private DBBlock dbBlock = new DBBlock();
	private DBSchedule dbSchedule = new DBSchedule();
	private boolean retrieveAssociation = true;
	
	@BeforeClass
	public static void prepareTest() throws Exception {
		
		Teacher t = new DBTeacher().findById(1); 
		new DBClass().insert("men0813", "1", "2013", t);
		
	}
	
	@AfterClass
	public static void cleanUp() throws Exception {
		
		DBClass dbCl = new DBClass();
		dbCl.remove(dbCl.find("men0813", false).getId());
		
	}
	
	@Before
	public void setUp() throws Exception {
		
		testInsert();
		
	}

	@After
	public void tearDown() throws Exception {
		
		testRemove();
		
	}

	@Test
	public void testDBBlock() {
		
		assertNotNull(this.dbBlock);
		
	}

	@Test
	public void testInsert() throws SQLException {
		
		assertTrue(new DBSchedule().insert("men0813", 36) > 0);
		
	}

	@Test
	public void testFind() throws Exception {
		
		assertTrue(this.dbBlock.find(this.dbSchedule.find("men0813", false).getId(), 1, retrieveAssociation) != null);
		
	}

	@Test
	public void testFindById() throws Exception {
		
		Block b = dbBlock.find(this.dbSchedule.find("men0813", false).getId(), 1, retrieveAssociation);
		assertTrue(this.dbBlock.findById(b.getId(), retrieveAssociation) != null);
		
	}

	@Test
	public void testFindBlocksByNumber() throws Exception {
		
		assertTrue(this.dbBlock.findBlocksByNumber(1, retrieveAssociation).size() > 0);
		
	}

	@Test
	public void testGetAll() throws Exception {
		
		assertTrue(this.dbBlock.getAll(retrieveAssociation).size() > 0);
		
	}

	@Test
	public void testGetAllBySchedule() throws Exception {
		
		Schedule sch = new DBSchedule().find("men0813", false);
		assertTrue(this.dbBlock.getAllBySchedule(sch.getId(), retrieveAssociation).size() > 0);
		
	}

	@Test
	public void testFindFilledBlocksByNumber() {
		
		assertTrue(this.dbBlock.findFilledBlocksByNumber(1, retrieveAssociation).size() > 0);
		
	}

	@Test
	public void testGetAllNonConflictingBySchedule() {
		
		Schedule sch = new DBSchedule().find("men0813", false);
		assertTrue(this.dbBlock.getAllNonConflictingBySchedule(sch.getId(), retrieveAssociation, 2).size() > 0);
		
	}

	@Test
	public void testUpdate() throws Exception {
		
		Schedule sch = new DBSchedule().find("men0813", false);
		TeachingBlock tb = new DBTeachingBlock().find("3HisL01-1", true);
		Block b = new DBBlock().find(sch.getId(), 2, true);
		assertTrue(this.dbBlock.update(sch.getId(), b.getBlockNumber(), tb) > 0);
		
	}
	
	@Test
	public void testUpdateWithNull() throws Exception {
		
		Schedule sch = new DBSchedule().find("men0813", false);
		TeachingBlock tb = null;
		Block b = new DBBlock().find(sch.getId(), 36, true);
		assertTrue(this.dbBlock.update(sch.getId(), b.getBlockNumber(), tb) > 0);
		
	}
	
	@Test
	public void testRemove() {
		
		assertTrue(new DBSchedule().remove("men0813") > 0);
		
	}

}
